<?php

require_once 'Pipes/Dispatcher.php';
require_once 'Pipes/FileSystem/Controller/Finder.php';
require_once 'Pipes/FileSystem/Controller/Loader.php';
require_once 'Pipes/User/Credentials/Factory.php';
require_once 'PipesImpl/Config.php';
require_once 'PipesImpl/Route.php';


class Pipes_Framework
{
	private $_config = null;
	
	public function __construct(PipesImpl_Config $config) {
		$this->_config = $config;
	}
	
	public function run(PipesImpl_Route $route) {
		$loader = new Pipes_FileSystem_Controller_Loader(
			new Pipes_FileSystem_Controller_Finder($this->_config)
		);

		$credFactory = new Pipes_User_Credentials_Factory();
		$credentials = $credFactory->fromSession();

		$loader->load($route->controller);
		$controller = new $loader->lastLoaded($this->_config);

		$dispatcher = new Pipes_Dispatcher($this->_config, $controller);
		return $dispatcher->dispatch($route, $credentials);
	}
}
